package cn.stylefeng.guns.modular.financial.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.financial.entity.FinancialIncomeMatching;
import cn.stylefeng.guns.modular.financial.model.params.FinancialIncomeMatchingParam;
import cn.stylefeng.guns.modular.financial.model.result.FinancialIncomeMatchingResult;
import cn.stylefeng.guns.modular.financial.service.FinancialIncomeMatchingService;
import cn.stylefeng.guns.modular.transport.entity.TransportDispatch;
import cn.stylefeng.guns.modular.transport.model.params.TransportDispatchParam;
import cn.stylefeng.guns.modular.transport.service.TransportDispatchService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


/**
 * 收入匹配管理表控制器
 *
 * @author zhucx
 * @Date 2024-03-27 15:58:48
 */
@Controller
@RequestMapping("/financialIncomeMatching")
public class FinancialIncomeMatchingController extends BaseController {

    private String PREFIX = "/financial/financialIncomeMatching";

    @Autowired
    private FinancialIncomeMatchingService financialIncomeMatchingService;

    @Autowired
    private TransportDispatchService transportDispatchService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2024-03-27
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	
    	BasicCarrierParam basicCarrierParam = new BasicCarrierParam();
    	basicCarrierParam.setDeliverId(deliverId);
    	model.addAttribute("carrierlist", basicCarrierService.findListBySpec(basicCarrierParam, user.getOperateId()));
        return PREFIX + "/financialIncomeMatching.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2024-03-27
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/financialIncomeMatching_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2024-03-27
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/financialIncomeMatching_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2024-03-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FinancialIncomeMatchingParam financialIncomeMatchingParam) {
        this.financialIncomeMatchingService.add(financialIncomeMatchingParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2024-03-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FinancialIncomeMatchingParam financialIncomeMatchingParam) {
        this.financialIncomeMatchingService.update(financialIncomeMatchingParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2024-03-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FinancialIncomeMatchingParam financialIncomeMatchingParam) {
        this.financialIncomeMatchingService.delete(financialIncomeMatchingParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2024-03-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FinancialIncomeMatchingParam financialIncomeMatchingParam) {
        FinancialIncomeMatching detail = this.financialIncomeMatchingService.getById(financialIncomeMatchingParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 导入页面
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	return PREFIX + "/import.html";
    }
    
    /**
     * 下载模板
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcel")
    public void downloadExcel(HttpServletRequest request,HttpServletResponse response) throws ServletException ,IOException {
        String path = "assets/excel/financialIncomeMatching.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream  outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("收入匹配模板" + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入excel   
     */
	@RequestMapping(value = "/importExcel" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData importExceNewmethodl(@RequestPart("file") MultipartFile file,  FinancialIncomeMatchingParam financialIncomeMatchingParam)  {
		ResponseData data = new ResponseData();
		try {
			data = financialIncomeMatchingService.getExceldata(file,financialIncomeMatchingParam,data);//处理导入数据
		} catch (Exception e) {
			data.setSuccess(false);
			e.printStackTrace();
		}
        return data;
    }

    /**
     *匹配接口
     *
     * @author zhucx
     * @Date 2024-03-27
     */
    @RequestMapping("/pipei")
    @ResponseBody
    public ResponseData pipei(FinancialIncomeMatchingParam financialIncomeMatchingParam) {
    	financialIncomeMatchingParam.setState("未匹配");
    	List<FinancialIncomeMatchingResult> list = financialIncomeMatchingService.findListBySpec(financialIncomeMatchingParam);
    	for (FinancialIncomeMatchingResult r : list) {
    		boolean rows = transportDispatchService.lambdaUpdate()
    		.eq(TransportDispatch::getCustomerOrder, r.getCustomerOrder())
    		.set(TransportDispatch::getIncomeState, "已匹配")
    		.update();
    		if (rows) {
    			FinancialIncomeMatchingParam update = new FinancialIncomeMatchingParam();
    			update.setId(r.getId());
    			update.setState("已匹配");
    			financialIncomeMatchingService.update(update);
			}
		}
        return ResponseData.success();
    }
    
    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2024-03-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FinancialIncomeMatchingParam financialIncomeMatchingParam,String condition ) {
    	User user = userService.getcurrentUser();
    	if (financialIncomeMatchingParam.getHdTime() == null){
    		
    		financialIncomeMatchingParam.setHdTime(DateUtil.getYearMonthByMonth(DateUtil.getMonth(), -1));
		}
    	financialIncomeMatchingParam.setDeptId(user.getDeptId());
        return this.financialIncomeMatchingService.findPageBySpec(financialIncomeMatchingParam,condition,user.getOperateId());
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2024-03-27
     */
    @ResponseBody
    @RequestMapping("/pplist")
    public LayuiPageInfo pplist(TransportDispatchParam transportDispatchParam,String condition ) {
    	User user = userService.getcurrentUser();
    	if (transportDispatchParam.getStartDate() == null){
    		transportDispatchParam.setStartDate(DateUtil.getYearMonthByMonth(DateUtil.getMonth(), -1));
		}
    	transportDispatchParam.setDeptId(user.getDeptId());
        return this.transportDispatchService.findPpPageBySpec(transportDispatchParam,condition,user.getOperateId());
    }

}


